<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

use Think\Controller;

/**
 * 前台公共控制器
 * 为防止多分组Controller名称冲突，公共Controller名称统一使用分组名称
 */
class HomeController extends Controller
{
    public $img_url = "";
    public $APPLET_APPID = "wx7bffcd11b785251e";                        //小程序 appid
    public $APPLET_SECRET = "e1f5bca22a5e430fa3d44822d9d803d2";         //小程序 secret
    protected function _initialize(){
        if($_SERVER['PHP_SELF'] == '/index.php'){
            $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
            header("Location: {$protocol}.{$_SERVER['SERVER_NAME']}/market/");
            exit;
        }
        $this->img_url = $_SERVER['SERVER_NAME'];

    }


    /**
     * 接口抛出异常信息
     */
    public function throw_exception($msg = "", $code = 500,$data = [])
    {
        $returnData['code'] = $code;
        $returnData['msg']  = $msg;
        $returnData['data'] = $data;
        exit(json_encode($returnData));
    }


    public function httpRequest($url, $data = '', $method = 'GET', $HTTPHEADER = '')
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        if ($method == 'POST') {
            curl_setopt($curl, CURLOPT_POST, 1);
            if ($data != '') {
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
        }
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        if ($HTTPHEADER) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $HTTPHEADER);
        } else {
            curl_setopt($curl, CURLOPT_HEADER, 0);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }
}
